package com.swak.threads;

import java.util.List;
import java.util.concurrent.TimeUnit;

import com.codahale.metrics.MetricRegistry;
import com.swak.metrics.impl.CodahaleMetricsFactory;
import com.swak.metrics.impl.MapMeta;
import com.swak.reactivex.threads.Contexts;
import com.swak.reactivex.threads.WorkerContext;

/**
 * 多线程的测试模式
 * 
 * @author DELL
 */
public class MultiThreadPool {

	public static void main(String[] args) throws InterruptedException {
		CodahaleMetricsFactory metricsFactory = new CodahaleMetricsFactory(new MetricRegistry()).setMethodOpen(false)
				.setMethodCollectAll(false).setPoolOpen(true);
		Contexts.setMetricsFactory(metricsFactory);
		MetricsDumpService dump = new MetricsDumpService(metricsFactory.metricRegistry());
		WorkerContext context = Contexts.createWorkerContext("Test-", 10, false, 1000, TimeUnit.SECONDS);
		for (int i = 0; i < 1000; i++) {
			context.execute(new Run1sTask());
			context.execute(new RunBlock100msTask());
			context.execute(new RunBlock200msTask());
			context.execute(new Run10msTask());
			context.execute(new Run50msTask());
		}

		// 5s 之后输出统计信息
		Thread.sleep(10000);
		List<MapMeta> metas = dump.metricsDump(null);
		for (MapMeta meta : metas) {
			System.out.println(meta);
		}
	}
}
